package cn.ximart.passport.controller;


import cn.ximart.passport.pojo.dto.AdminAddNewDTO;
import cn.ximart.passport.pojo.dto.RoleAddNewDTO;
import cn.ximart.passport.pojo.vo.RoleListItemVO;
import cn.ximart.passport.security.LoginPrincipal;
import cn.ximart.passport.service.IRoleService;
import cn.ximart.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "角色管理")
@RequestMapping("/roles")
@RestController
@CrossOrigin
public class RoleController {

    @Autowired
    private IRoleService roleService;


    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody RoleAddNewDTO roleAddNewDTO) {
        log.debug("接收到的请求数据为:{}", roleAddNewDTO);
        roleService.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/ams/admin/update')")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("准备开始处理删除角色业务，接受到的参数为：{}",id);
        roleService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询角色列表")
    @GetMapping("")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult roleList(@AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("接收到查询角色列表的请求,开始执行");
        List<RoleListItemVO> list = roleService.list();
        return JsonResult.ok(list);
    }

}
